import java.awt.*;
import java.awt.event.*;
 
public class ChoiceDemo implements ItemListener {
    Frame frame;
    Choice choice;
	String[] tmpString;

    Checkbox checkbox1, checkbox2, checkbox3;
	StringProcess sp=new StringProcess();	
	String seperateString="[\\p{Punct}]";


    public static void main(String[] args) {
        new ChoiceDemo();
    }
     
    public ChoiceDemo() {
	String tmpString="";
        frame = new Frame("AWTDemo");
        frame.addWindowListener(new AdapterDemo());
        frame.setLayout(new FlowLayout());
         //---------------------------------------------------
        choice = new Choice();
        choice.addItemListener(this);
        choice.add("one");
        choice.add("two");
        choice.add("three");
        choice.add("remove all");
        choice.select("three");

		tmpString=choice.toString();//not use this cause index wrong?
        frame.add(choice);
         
	//---------------------------------------------------
	checkbox1 = new Checkbox("c1");
        checkbox1.addItemListener(this);
		tmpString=checkbox1.toString();//not use this cause index wrong?
        checkbox2 = new Checkbox("c2");
        checkbox2.setState(true);
        checkbox2.addItemListener(this);
		tmpString=checkbox2.toString();//not use this cause index wrong?
        checkbox3 = new Checkbox("c3");
        checkbox3.addItemListener(this);
	         tmpString=checkbox3.toString();//not use this cause index wrong?

        frame.add(checkbox1);
        frame.add(checkbox2);
        frame.add(checkbox3);
	//---------------------------------------------------


        frame.pack();
        frame.setVisible(true);
         
//        for (int i = 0;i < choice.getItemCount();i++) {
  //          System.out.println(i + " : " + choice.getItem(i));
    //    }
    }
     
    public void itemStateChanged(ItemEvent e) {
	Choice choice;
	Checkbox checkbox;

//	System.out.println(e.getSource());

	tmpString=sp.getStringArray(e.getSource().toString(),seperateString,null);

//	System.out.println(tmpString[3]);

	switch(tmpString[3]){
		case "choice0":

			break;
		case "checkbox0":
		case "checkbox1":
		case "checkbox2":

		default:
			break;
	}


	switch(tmpString[3]){
		case "choice0":
			choice = (Choice) e.getSource();
			if (choice.getSelectedItem() == "one") {
				choice.insert("demo", 1);
			}       
			if (choice.getSelectedItem() == "two") {
				choice.remove("one");
			}
			if (choice.getSelectedItem() == "three") {
				choice.remove(1);
			}
			if (choice.getSelectedItem() == "remove all") {
				choice.removeAll();
			}
			break;
		case "checkbox0":
		        checkbox= (Checkbox) e.getSource();
			if(e.getStateChange() == ItemEvent.SELECTED){
				checkbox.setLabel("off");
			}
			else{
				checkbox.setLabel("c1");
			}
			break;
		case "checkbox1":
		        checkbox= (Checkbox) e.getSource();
			if(e.getStateChange() == ItemEvent.SELECTED){
				checkbox.setLabel("off");
			}
			else{
				checkbox.setLabel("c2");
			}
			break;
		case "checkbox2":
		        checkbox= (Checkbox) e.getSource();
			if(e.getStateChange() == ItemEvent.SELECTED){
				checkbox.setLabel("off");
			}
			else{
				checkbox.setLabel("c3");
			}
			break;
		default:
			break;
	}
    }
}
 
class AdapterDemo extends WindowAdapter {
    public void windowClosing(WindowEvent e) {
        System.exit(0);
    }
}